package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReplyActivity;
import com.thinkcube.edutainment.login.WebViewActivity.WebAppInterface;

@EFragment(R.layout.html_frgment)
public class HtmlFragment extends Fragment {

	@ViewById(R.id.webView1)
	WebView webView;

	@ViewById(R.id.progressBar)
	ProgressBar progres;

	public class WebAppInterface {
		Context mContext;

		/** Instantiate the interface and set the context */
		WebAppInterface(Context c) {
			mContext = c;
		}

		/** Show a toast from the web page */
		@JavascriptInterface
		public void showToast(String toast) {
			showMessge(""+toast);
			//showMessge("confirmed");
			if(toast.equals("true")){
				getActivity().finish();
				
			}
			else{
				getFragmentManager().popBackStack();
			}
		}
	}
	
	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), ""+ msg, Toast.LENGTH_LONG).show();
	}

	@AfterViews
	void iniUi() {
		

		webView.addJavascriptInterface(new WebAppInterface(getActivity()),
				"Android");

		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// Log.i(TAG, "Processing webview url click...");
				view.loadUrl(url);
				return true;
			}

			public void onPageFinished(WebView view, String url) {
				// Log.i(TAG, "Finished loading URL: " +url);
				if (progres.getVisibility() == View.VISIBLE) {
					progres.setVisibility(View.GONE);
				}
			}

			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// Log.e(TAG, "Error: " + description);
				/*
				 * Toast.makeText(activity, "Oh no! " + description,
				 * Toast.LENGTH_SHORT).show();
				 * alertDialog.setTitle("Error");
				 * alertDialog.setMessage(description);
				 * alertDialog.setButton("OK", new
				 * DialogInterface.OnClickListener() { public void
				 * onClick(DialogInterface dialog, int which) { return; }
				 * }); alertDialog.show();
				 */
			}
		});

		if (getArguments().containsKey("Url")) {
			webView.getSettings().setJavaScriptEnabled(true);
			webView.getSettings()
					.setJavaScriptCanOpenWindowsAutomatically(true);

			// webView.loadUrl("http://www.google.com");

			webView.loadUrl((getArguments().getString("Url")));
		} else if (getArguments().containsKey("content")) {
			webView.getSettings().setJavaScriptEnabled(true);
			// String summary = "<html><body>Dummy Html conent</body></html>";
			String textData = getArguments().getString("content");
			webView.loadData(textData, "text/html", "utf-8");
			//webView.loadUrl("https://guru.dialog.lk/user/test");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return null;
	}
}
